package com.tenement.controller.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
import com.tenement.mybatis.entity.User;
import com.tenement.service.interfaces.IUserService;

/**
 * 邮箱验证码,校验
 * 
 * @author wk
 *
 */
@Controller
public class EmailCodeController {

	private static final Log logger = LogFactory.getLog(EmailCodeController.class);

	@Autowired
	private IUserService iUserService;

	String code = "6666";// 邮箱验证码
	Map<String, String> emailMap = new HashMap<String, String>();// 获取验证码时，将发送验证码的邮箱存入到Map中，在提交数据时和Map中的邮箱进行对比。

	/**
	 * 发送到客户邮箱的验证码,需要修改验证码存到session中
	 * 
	 * @param emailaddress
	 * @return
	 */
	@RequestMapping(value = "/getEmailVerifyCode.action")
	public @ResponseBody String getEmailVerifyCode(@RequestBody String email) {

		logger.info("【-前台传来的邮箱地址：" + email + "-】");
		if ("".equals(email) || email == null) {
			try {
				throw new Exception("【-传入的用户邮箱为空-】");
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			// code = EmailSecurityCodeUtil.getEmailSecurityCode(emailaddress);
			emailMap.put("email", email);
			emailMap.put("code", code);
		}
		return code;
	}

	/**
	 * 绑定用户邮箱 u_id u_email emailCode
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping("/modifyEmail.action")
	public @ResponseBody String modifyEmail(@RequestBody String data,HttpServletRequest request) {
		JSONObject jsonstr = JSON.parseObject(data);
		String u_id = jsonstr.getString("u_id"); // 用户ID
		String u_email = jsonstr.getString("u_email");// 用户修改的手机号
		String emailCode = jsonstr.getString("emailCode");// 手机验证码
		User user = new User();
		try {
			if (u_id == null || "".equals(u_id)) {
				throw new ParameterException("【用户uId为空】");
			}
			if (u_email == null || "".equals(u_email)) {
				throw new ParameterException("【用户u_email为空】");
			}
			if (emailCode == null || "".equals(emailCode)) {
				throw new ParameterException("【用户emailCode为空】");
			}
		} catch (ParameterException e) {
			e.printStackTrace();
		}
		if (!u_email.equals(emailMap.get("email"))) {
			return "-1";// 提示用户重新获取验证码
		} else if (!emailCode.equals(emailMap.get("code"))) {
			return "-2";// 用户输入的验证码错误
		} else {
			user.setuId(Integer.parseInt(u_id));
			user.setuEmail(u_email);
			iUserService.modifyUserEmail(user,request);
			;
			return state.Success;// 修改成功,1
		}
	}

	/**
	 * 校验用户输入的邮箱验证码,在身份验证时需要此方法
	 * 
	 * @param data
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/verifyEmailCode.action")
	public @ResponseBody String verifyEmailCode(@RequestBody String data, HttpSession session) {
		JSONObject jsonstr = JSON.parseObject(data);
		String u_email = jsonstr.getString("u_email"); // u_email emailCode
		String emailCode = jsonstr.getString("emailCode");
		logger.info("【验证用户身份】" + u_email + " " + emailCode);
		try {
			if (emailCode == null || "".equals(emailCode)) {
				throw new Exception("【-用户输入的验证码为空-】");
			}
			if (u_email == null || "".equals(u_email)) {
				throw new Exception("【-用户输入的邮箱账号为空-】");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (emailCode.equals(emailMap.get("code")) && u_email.equals(emailMap.get("email"))) {
			return state.Success;// 用户输入的手机验证码正确
		} else {
			return state.Failed;// 用户输入的手机验证码错误
		}
	}

	/**
	 * 验证用户邮箱 u_id u_email emailCode
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping("/verifyEmailCode2.action")
	public @ResponseBody String verifyEmailCode2(@RequestBody String data) {
		JSONObject jsonstr = JSON.parseObject(data);
		String u_id = jsonstr.getString("u_id"); // 用户ID
		String u_email = jsonstr.getString("u_email");// 用户修改的手机号
		String emailCode = jsonstr.getString("emailCode");// 手机验证码
		try {
			if (u_id == null || "".equals(u_id)) {
				throw new Exception("【用户uId为空】");
			}
			if (u_email == null || "".equals(u_email)) {
				throw new Exception("【用户u_email为空】");
			}
			if (emailCode == null || "".equals(emailCode)) {
				throw new Exception("【用户emailCode为空】");
			}
		} catch (Exception e) {
			logger.error("【修改用户绑定的邮箱参数为空】");
		}
		if (!u_email.equals(emailMap.get("email"))) {
			return "-1";// 提示用户重新获取验证码
		} else if (!emailCode.equals(emailMap.get("code"))) {
			return "-2";// 用户输入的验证码错误
		} else {
			return state.Success;// 修改成功,1
		}
	}
}
